package com.haiwen.controller;
import com.haiwen.pojo.users;
import com.haiwen.service.registerLoginService;
import com.haiwen.utils.UserContext;
import com.haiwen.vo.responseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RestController
public class usersController{
    @Autowired
    registerLoginService service;
    @Autowired
    private JavaMailSender javaMailSender;//在spring中配置的邮件发送的bean
    @PostMapping(value = "/regist.do") //要替换成postMapping 此处仅作测试
    public responseData regist(users user,
                               String sessionCode,
                               HttpServletRequest request) throws RuntimeException{
        HttpSession session = request.getSession();

        Object attribute = session.getAttribute(UserContext.VERIFICATION_CODE);
        if (sessionCode.equals("")){
            return new responseData(500,"请输入验证码！");
        }
        if (attribute==null){
            return new responseData(500,"请获取验证码！");
        }
        if (attribute.toString().equals(sessionCode)&& service.regist(user)>=1){
            session.removeAttribute(UserContext.VERIFICATION_CODE);
            return new responseData(200,"注册成功");
       }else if (!attribute.toString().equals(sessionCode)){
            return new responseData(500,"验证码错误");
        }
        session.removeAttribute(UserContext.VERIFICATION_CODE);
        return new responseData(500,"注册失败,此账号已存在");

    }
    //管理员登录
    @PostMapping(value = "/rootLogin.do") //要替换成postMapping 此处仅作测试
    public responseData rootLogin(@RequestParam("uname") String uname,
                              @RequestParam("upass") String unpass,
                              HttpSession session
                                  ) throws RuntimeException{
        int login = service.Login(uname,"0",unpass);
        if (login == 2) {
            session.setAttribute(UserContext.USER_IN_SESSION,login);
            return new responseData(200, "登录成功");
        }
        return new responseData(500, "登录失败");
    }
    //普通用户登录
    @PostMapping(value = "/login.do") //要替换成postMapping 此处仅作测试
    public responseData login(@RequestParam("uname") String uname,
                              @RequestParam("upass") String unpass,
                              HttpSession session,
                              HttpServletRequest request
    ) throws RuntimeException{
        int login = service.Login(uname,"1",unpass);
        switch (login){
            case 1:
                return new responseData(500,"登录失败，账号密码错误！");
            case 2:
                return new responseData(200,"登录成功");
            case 3:
                return new responseData(500,"登录失败，请两分钟后尝试！");
            default:
                return new responseData(500,"账户未注册！");
        }
    }
}
